package com.lpgu.xml.repositories;

import com.lpgu.xml.pojo.Customer;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author: lpgu
 * @describe:CustomerRepository
 * @createAt: 2021/12/28 21:53
 */
public interface CustomerRepository extends/* CrudRepository<Customer,Long>,*/ PagingAndSortingRepository<Customer,Long> {
 //==============Crud
// @Query("SELECT Customer FROM Customer where custName=?1")
    @Query("FROM Customer where custName=?1")
    Customer findCustomerByCustName(String custom);

    /*@Query("FROM Customer where custName=:cu")
    Customer findCustomerByCustName(@Param("cu") String custom);*/

    @Modifying //通知springfdata是增删改操作
    @Query("UPDATE Customer SET custName=?1 where id=?2")
    int updateCustomer(String name,long id);

   //====================原始sql

    @Query(nativeQuery = true,value = "select * FROM Customer limit 2")
    Customer findCustomerByCustNameNative(String custom);

//JPQL不支持插入，但是又特殊的插入
   @Transactional
   @Modifying
   @Query("Insert into Customer(custName) SELECT c.custName FROM Customer c where c.custId=?1")
   int insertCustomerBySelect(Long id);


}
